#!/bin/bash
# Changes
# - 20041211 disallow gnome to change brightness -> fixes the brightness
#            distribute xandros display driver to get skype 2.0 video to work
# - 20071208 disabled overclocking on startup and resume
#            added depmod -a so that the modules are available right away
#            added gnome notifications for wifi and overclocking

# if no argument is given, show usage 
if [ $# == 0 ]
then
	echo "USAGE: sudo $0 ("`grep '^[a-z0-9]*)' $0 | sed 's/).*//'|tr "\n" "|"`"all)"
	exit 1
fi

# ensure that the script is run as root
if [ `id -u` != 0 ]
then
	echo "ERROR: this script is intended to be run as root"
	exit 1
fi

# call with all possible options
if [ $1 == all ]
then
	exec $0 `grep '^[a-z0-9]*)' $0 | sed 's/).*//'`
fi

BASE=/
#BASE=./contents

INSTALL="install --backup=numbered"

# for each argument, check the execution list
while [ $# != 0 ]
do

case "$1" in
modules)
	# install modules
	echo "* Installing modules for kernel 2.6.22-14-generic"
	for file in `find modules -type f`
	do
		destination=`echo $file|sed 's/^modules//'`
		$INSTALL -m 0644 -D $file $destination
	done
	rm /lib/modules/`uname -r`/madwifi/*
	depmod -a
	;;
acpi)
	# install acpi support
	echo "* Installing ACPI support (suspend, hotkeys...)"
	$INSTALL -m 0644 acpi/etc/acpi/events/eee-* $BASE/etc/acpi/events
	mkdir -p $BASE/etc/acpi/events.ignore
	# move backups and conflicting files out of the way
	for FILE in `find $BASE/etc/acpi/events/ -name '*eee*~'`
	do
		mv $FILE $BASE/etc/acpi/events.ignore
	done
	$INSTALL -m 0644 acpi/etc/modules $BASE/etc/modules
	$INSTALL -m 0644 acpi/etc/default/acpi-support $BASE/etc/default
	$INSTALL -m 0755 acpi/etc/acpi/appbtn.sh $BASE/etc/acpi
	$INSTALL -m 0755 acpi/etc/acpi/notify-wifi.py $BASE/etc/acpi
	$INSTALL -m 0755 acpi/etc/acpi/notify-overclock.py $BASE/etc/acpi
	$INSTALL -m 0755 acpi/etc/gdm/PostLogin/Default $BASE/etc/gdm/PostLogin
	$INSTALL -m 0644 acpi/etc/xmodmaprc $BASE/etc/xmodmaprc
    killall -HUP acpid
	# disallow gnome to change the brightness (this is handled by the bios)
	if [ -f /usr/lib/hal/scripts/linux/hal-system-lcd-set-brightness-linux ]
	then
		mv /usr/lib/hal/scripts/linux/hal-system-lcd-set-brightness-linux \
			/usr/lib/hal/scripts/linux/hal-system-lcd-set-brightness-linux.disabled
	fi
	;;
clock)
	# install fsb overclocking
	echo "* Installing overclocking (activate using Fn-F6)"
	$INSTALL -m 0755 clock/etc/init.d/eee-overclock.sh $BASE/etc/init.d
	$INSTALL -m 0755 clock/etc/acpi/suspend.d/56-eee-downclock.sh $BASE/etc/acpi/suspend.d
	#$INSTALL -m 0755 clock/etc/acpi/resume.d/60-eee-overclock.sh $BASE/etc/acpi/resume.d
	#ln -sf $BASE/etc/init.d/eee-overclock.sh $BASE/etc/rc2.d/S10eee-overclock
	;;
wifi)
	# install wifi
	echo "* Installing wifi support (for HAL and Network Manager)"
	$INSTALL -m 0755 wifi/sbin/eee-wifi-on-off.sh $BASE/sbin
	$INSTALL -m 0644 wifi/etc/default/linux-restricted-modules-common $BASE/etc/default/
	;;
sound)
	# install sound
	echo "* Installing sound-related fixes (shutdown, games...)"
	$INSTALL -m 0644 sound/etc/modprobe.d/alsa-base $BASE/etc/modprobe.d
	$INSTALL -m 0755 sound/etc/init.d/halt $BASE/etc/init.d
	$INSTALL -m 0644 sound/etc/openalrc $BASE/etc
	;;
init)
	# install init
	echo "* Installing init scripts (faster init)"
	$INSTALL -m 0755 init/etc/init.d/rc $BASE/etc/init.d
	if [ -f $BASE/etc/rc2.d/S12hal ]
	then
		mv $BASE/etc/rc2.d/S12hal $BASE/etc/rc2.d/S13hal
		mv $BASE/etc/rc3.d/S12hal $BASE/etc/rc3.d/S13hal
		mv $BASE/etc/rc4.d/S12hal $BASE/etc/rc4.d/S13hal
		mv $BASE/etc/rc5.d/S12hal $BASE/etc/rc5.d/S13hal
	fi
	;;
sources)
	echo "* Enabling software sources"
	for source in main universe restricted multiverse
	do
		software-properties-gtk -n -e $source > /dev/null 2>&1
	done
	;;
x11)
	echo "* Installing Xandros display driver (for skype)"
	$INSTALL -m 0755 x11/usr/lib/xorg/modules/drivers/intel_drv.so $BASE/usr/lib/xorg/modules/drivers/
	;;
*)
	echo "ERROR: unrecognized argument \"$2\""
	exit 1
;;
esac
# remove processed argument
shift
done

echo "Done."
